
package com.yilin.tms.order.controller;

import com.yilin.commons.exception.BusinessException;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.SafeVerify;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.entity.order.clearing.GatherRecord;
import com.yilin.tms.core.entity.order.clearing.ClearingDetail;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.order.service.clearing.IClearingService;
import com.yilin.tms.order.service.clearing.IGatherService;
import com.yilin.tms.order.service.order.ITransitService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 清算控制器
 * 汇总控制器
 *
 * @author zhangjinyuan
 * @date 2022/10/31
 */
@RestController
@RequestMapping("/order/clearing/")
public class ClearingController extends BaseController {

    @Resource
    IClearingService clearingService;
    @Resource
    IGatherService gatherService;
    @Resource
    ITransitService transitService;


    /*----------------------------------------------汇总部分--------------------------------------------------*/

    /**
     * 获取待汇总运单列表
     */
    @PostMapping(value = "getGatherTransitOrderList")
    @UserTarget(UserType.platformUser)
    public ReturnData<List<TransitOrder>> getGatherTransitOrderList(String shipperOrganizeId, String robOrganizeId,
                                                                    String receiptTimeRange, Boolean matching, Boolean includedTax, String transitTruckNo) {
        List<TransitOrder> list = transitService.getGatherTransitOrderList(getLoginUser(), shipperOrganizeId, robOrganizeId, receiptTimeRange, matching, includedTax, transitTruckNo);
        return jsonView(SUCCESS_CODE, "获取运单列表成功", list);
    }

    /**
     * 获取需要汇总货主下拉列表
     */
    @PostMapping(value = "getGatherShipperDropList")
    @UserTarget(UserType.platformUser)
    public ReturnData<List<Map<String, Object>>> getGatherShipperDropList() {
        List<Map<String, Object>> list = transitService.getGatherShipperDropList(getLoginUser());
        return jsonView(SUCCESS_CODE, "获取需要汇总货主下拉列表成功", list);
    }

    /**
     * 获取需要汇总车主下拉列表
     */
    @PostMapping(value = "getGatherTruckerDropList")
    @UserTarget(UserType.platformUser)
    public ReturnData<List<Map<String, Object>>> getGatherTruckerDropList() {
        List<Map<String, Object>> list = transitService.getGatherTruckerDropList(getLoginUser());
        return jsonView(SUCCESS_CODE, "获取需要汇总车主下拉列表成功", list);
    }

    /**
     * 获取平台汇总记录
     */
    @PostMapping(value = "getGatherRecordPage")
    @NotNull({"page", "limit"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> getGatherRecordPage(GatherRecord query, Integer page, Integer limit) {
        PageData<GatherRecord> gatherRecordPage = gatherService.getGatherRecordPage(getLoginUser(), query, page, limit);
        return jsonView(SUCCESS_CODE, "创建汇总记录成功", gatherRecordPage);
    }

    /**
     * 创建汇总记录
     * 在列表头创建，可以随时作废和确认
     */
    @PostMapping(value = "createGatherRecord")
    @UserTarget(UserType.platformUser)
    @SafeVerify
    public ReturnData<Object> createGatherRecord(String[] transitOrderIds) {
        if (transitOrderIds == null || transitOrderIds.length == 0) return jsonErrView("请至少选择一条运单");
        List<TransitOrder> transitOrderList = transitService.listByIds(Arrays.asList(transitOrderIds));
        GatherRecord gatherRecord = gatherService.createGatherRecord(getLoginUser(), transitOrderList);
        return jsonView(SUCCESS_CODE, "创建汇总记录成功", gatherRecord);
    }

    /**
     * 作废汇总
     */
    @PostMapping(value = "scrapGatherRecord")
    @NotNull({"gatherRecordId"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> scrapGatherRecord(String gatherRecordId, @Explain("作废原因") String scrapReason) {
        gatherService.scrapGatherRecord(gatherRecordId, scrapReason, getLoginUser());
        return jsonView(SUCCESS_CODE, "作废成功");
    }

    /**
     * 获取汇总
     */
    @PostMapping(value = "getGatherRecordById")
    @NotNull({"gatherRecordId"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> getGatherRecordById(String gatherRecordId) {
        GatherRecord gatherRecord = gatherService.getById(gatherRecordId);
        return jsonView(SUCCESS_CODE, "获取汇总成功", gatherRecord);
    }

    /**
     * 更新汇总
     */
    @PostMapping(value = "updateGatherRecord")
    @NotNull({"id", "insuranceRate", "shipperTaxRate", "shipperFeeRate", "truckerFeeRate", "addedTaxRate", "cityTaxRate",
            "localEduTaxRate", "eduAddTaxRate", "incomeTaxRate", "gatherActualPayCost", "totalInsuranceCost",
            "gatherAdditionalCost", "gatherDiffIndemnityCost", "gatherOilCost", "gatherEtcTollCost"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> updateGatherRecord(GatherRecord fromRecord) {
        if (fromRecord.getGatherActualPayCost() == null) throw new BusinessException("汇总金额不能为空！");
        if (fromRecord.getGatherActualPayCost() <= 0) throw new BusinessException("汇总金额不能位0！");
        gatherService.updateGatherRecord(getLoginUser(), fromRecord);
        return jsonView(SUCCESS_CODE, "更新汇总成功");
    }

    /**
     * 确认汇总
     */
    @PostMapping(value = "sureGatherRecord")
    @NotNull({"gatherRecordId"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> sureGatherRecord(String gatherRecordId, Boolean sureClearing) {
        if (sureClearing == null) sureClearing = false;
        gatherService.sureGatherRecord(getLoginUser(), gatherRecordId, sureClearing);
        return jsonView(SUCCESS_CODE, "更新汇总成功");
    }


    /*----------------------------------------------清算部分--------------------------------------------------*/

    /**
     * 获取清算单/根据运单ID获取
     */
    @PostMapping(value = "getClearingDetailPage")
    @NotNull({"page", "limit"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> getClearingDetailPage(ClearingDetail query, Integer page, Integer limit) {
        PageData<ClearingDetail> clearingDetailPage = clearingService.getClearingDetailPage(getLoginUser(), query, page, limit);
        return jsonView(SUCCESS_CODE, "获取清算单成功", clearingDetailPage);
    }

    /**
     * 获取清算
     */
    @PostMapping(value = "getClearingDetailById")
    @NotNull({"clearingDetailId"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> getClearingDetailById(String clearingDetailId) {
        ClearingDetail clearingDetail = clearingService.getById(clearingDetailId);
        return jsonView(SUCCESS_CODE, "获取汇总成功", clearingDetail);
    }

    /**
     * 获取清算单/根据运单ID获取
     */
    @PostMapping(value = "getClearingDetailByTransitOrderId")
    @NotNull({"transitOrderId", "clearLock", "passCode"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> getClearingDetailByTransitOrderId(@Explain("运单ID") String transitOrderId) {
        ClearingDetail clearingDetail = clearingService.getClearingDetailByTransitOrderId(transitOrderId);
        return jsonView(SUCCESS_CODE, "获取清算单成功", clearingDetail);
    }

    /**
     * 清算锁定/解锁
     */
    @PostMapping(value = "clearingLock")
    @NotNull({"clearingDetailId", "lockStatus"})
    @UserTarget(UserType.platformUser)
    @SafeVerify
    public ReturnData<Object> clearingLock(@Explain("清算ID") String clearingDetailId, @Explain("是否锁定") ClearingDetail.LockStatus lockStatus) {
        clearingService.lockClearingDetail(getLoginUser(), clearingDetailId, lockStatus);
        return jsonView(true, lockStatus == ClearingDetail.LockStatus.lock ? "锁定成功" : "解锁成功");
    }

    /**
     * 清算费用更新
     */
    @PostMapping(value = "updateClearingDetail")
    @NotNull({"transitOrderId", "id"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> updateClearingDetail(ClearingDetail clearingDetail) {
        TransitOrder transitOrder = transitService.getById(clearingDetail.getTransitOrderId());
        clearingService.updateClearingDetail(getLoginUser(), transitOrder, clearingDetail);
        return jsonView(SUCCESS_CODE, "费用更新成功");
    }

    /**
     * 确认清算
     */
    @PostMapping(value = "sureClearingDetail")
    @NotNull({"clearingDetailId"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> sureClearingDetail(String clearingDetailId) {
        ClearingDetail clearingDetail = clearingService.getById(clearingDetailId);
        TransitOrder transitOrder = transitService.getById(clearingDetail.getTransitOrderId());
        clearingService.sureClearingDetail(getLoginUser(), transitOrder, clearingDetail);
        return jsonView(SUCCESS_CODE, "确认清算成功");
    }


}

